from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge, RidgeCV
from sklearn.metrics import mean_squared_error
# from sklearn.externals import joblib  # 这个是scikit-learn版本不大于0.20.3的方法，之后其将joblib分离了，想用joblib要按下面的方式导入
import joblib


def liner_model1():
    """
    正规方程法
    :return:
    """
    # 1. 收集数据
    data = load_boston()

    # 2. 数据处理
    x_train, x_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.3)

    # 3. 特征工程
    # 3.1 创建转化器
    transfer = StandardScaler()

    # 3.2 特征预处理
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.fit_transform(x_test)

    # 4. 机器学习
    # 4.1 创建线性回归的估计器（正规方程）
    estimator = LinearRegression()

    # 4.2 模型训练
    estimator.fit(x_train, y_train)

    # 5. 模型评估
    # 5.1 测试结果
    y_pre = estimator.predict(x_test)
    print('预测目标值为为:\n', y_pre)
    print('预测目标值和测试目标值比对:\n', y_pre == y_test)
    print('模型中的系数为:\n', estimator.coef_)
    print('模型中的偏执值为:\n', estimator.intercept_)

    # 5.2 评分
    score = estimator.score(x_test, y_test)
    print('准确率为:\n', score)

    # 5.2 均方根误差
    ret = mean_squared_error(y_test, y_pre)
    print('均方根误差为:\n', ret)


def liner_model2():
    """
    梯度下降发
    :return:
    """
    data = load_boston()

    x_train, x_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2)

    transfer = StandardScaler()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.fit_transform(x_test)

    estimator = SGDRegressor(max_iter=1000)
    estimator.fit(x_train, y_train)

    y_pre = estimator.predict(x_test)
    print('预测的目标值为:\n', y_pre)
    print('预测的目标值和测试目标值对比:\n', y_pre == y_test)
    print('模型中的系数为:\n', estimator.coef_)
    print('模型中的偏执值为:\n', estimator.intercept_)

    score = estimator.score(x_test, y_test)
    print('评分为:\n', score)

    ret = mean_squared_error(y_test, y_pre)
    print('均方根误差为:\n', ret)


def liner_model3():
    """
    领回归
    :return:
    """
    data = load_boston()

    # random_state是指定按照什么方式进行训练集合测试集的划分
    x_train, x_test, y_train, y_test = train_test_split(data.data, data.target, random_state=22, test_size=0.2)

    transfer = StandardScaler()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.fit_transform(x_test)

    # # 机器学习
    # # estimator = Ridge()
    # estimator = RidgeCV()
    # estimator.fit(x_train, y_train)
    # # 保存模型
    # joblib.dump(estimator, '../estimator/test.pkl')

    # 加载模型, 保存模型后，机器学习这块直接加载模型就行
    estimator = joblib.load('../estimator/test.pkl')

    y_pre = estimator.predict(x_test)
    print('预测的目标值为:\n', y_pre)
    print('预测的目标值和测试目标值对比:\n', y_pre == y_test)
    print('模型中的系数为:\n', estimator.coef_)
    print('模型中的偏执值为:\n', estimator.intercept_)

    score = estimator.score(x_test, y_test)
    print('评分为:\n', score)

    ret = mean_squared_error(y_test, y_pre)
    print('均方根误差为:\n', ret)


if __name__ == '__main__':
    # liner_model1()
    # liner_model2()
    liner_model3()


